package cn.elwy.rcp.ui.explorer.create.wizard;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import cn.elwy.common.util.AssertUtil;
import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.explorer.core.AppContext;
import cn.elwy.rcp.ui.explorer.core.ProjectConst;
import cn.elwy.rcp.ui.explorer.core.service.SvnManageService;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;
import cn.elwy.rcp.ui.wizard.BaseWizardPage;

public class ProjectSvnWizardPage extends BaseWizardPage {

	public static final String ID = "ProjectSvnWizardPage";

	private SvnManageService svnManageService = AppContext.getBean(SvnManageService.ID);

	private Text scmUrlText;
	private Label scmUrlLabel;
	private Text sourcePathText;
	private Text docPathPath;
	private Label docPathLabel;
	private Text dbscriptPathText;
	private Label demoPathLabel;
	private Text demoPathText;
	private Label containerPathLabel;
	private Text containerPathText;
	private Label parentPomPathLabel;
	private Text parentPomPathText;
	private Label startVersionLabel;
	private Composite composite_1;
	private Text startVersionText;
	private Label currentVersionLabel;
	private Text endVersionText;
	private Button headRevisionButton;
	private Button revisionButton;
	private Label svnUserLabel;
	private Composite composite;
	private Text svnUserText;
	private Label svnPasswordLabel;
	private Text svnPasswordText;
	private Button checkSvnButton;

	private ProjectConfig projectConfig;

	private boolean pageState;
	private Text toolsPathText;
	private Label toolsPathLabel;
	private Label label;
	private Text srcPathText;

	/**
	 * Create the wizard.
	 */
	public ProjectSvnWizardPage() {
		super(ID);
		setTitle("源码信息配置");
		setDescription("SVN信息配置和源码目录设置");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = createUI(parent);
		setControl(container);

		addListener();
		init();
	}

	private Composite createUI(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new GridLayout(3, false));

		scmUrlLabel = new Label(container, SWT.NONE);
		scmUrlLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		scmUrlLabel.setText("SVN地址：");

		scmUrlText = new Text(container, SWT.BORDER);
		scmUrlText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		svnUserLabel = new Label(container, SWT.NONE);
		svnUserLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		svnUserLabel.setText("SVN用户名：");

		composite = new Composite(container, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		GridLayout gl_composite = new GridLayout(4, false);
		gl_composite.marginWidth = 0;
		gl_composite.marginHeight = 0;
		composite.setLayout(gl_composite);

		svnUserText = new Text(composite, SWT.BORDER);
		GridData gd_svnUserText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_svnUserText.widthHint = 100;
		svnUserText.setLayoutData(gd_svnUserText);

		svnPasswordLabel = new Label(composite, SWT.NONE);
		svnPasswordLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		svnPasswordLabel.setText("SVN密码：");

		svnPasswordText = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		svnPasswordText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		checkSvnButton = new Button(composite, SWT.CHECK);
		checkSvnButton.setText("登录验证");

		startVersionLabel = new Label(container, SWT.NONE);
		startVersionLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		startVersionLabel.setText("基线版本：");

		composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		GridLayout gl_composite_1 = new GridLayout(5, false);
		gl_composite_1.marginWidth = 0;
		gl_composite_1.marginHeight = 0;
		composite_1.setLayout(gl_composite_1);

		startVersionText = new Text(composite_1, SWT.BORDER);
		GridData gd_startVersionText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_startVersionText.widthHint = 100;
		startVersionText.setLayoutData(gd_startVersionText);

		currentVersionLabel = new Label(composite_1, SWT.NONE);
		currentVersionLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		currentVersionLabel.setText("当前版本：");

		headRevisionButton = new Button(composite_1, SWT.RADIO);
		headRevisionButton.setSelection(true);
		headRevisionButton.setText("HEAD");

		revisionButton = new Button(composite_1, SWT.RADIO);
		revisionButton.setText("Revision");

		endVersionText = new Text(composite_1, SWT.BORDER);
		GridData gd_endVersionText = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_endVersionText.widthHint = 100;
		endVersionText.setLayoutData(gd_endVersionText);

		Label sourcePathLabel = new Label(container, SWT.NONE);
		sourcePathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		sourcePathLabel.setText("源码目录：");

		sourcePathText = new Text(container, SWT.BORDER);
		sourcePathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		docPathLabel = new Label(container, SWT.NONE);
		docPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		docPathLabel.setText("文档目录：");

		docPathPath = new Text(container, SWT.BORDER);
		docPathPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		demoPathLabel = new Label(container, SWT.NONE);
		demoPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		demoPathLabel.setText("演示目录：");

		demoPathText = new Text(container, SWT.BORDER);
		demoPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("发布源码：");

		srcPathText = new Text(container, SWT.BORDER);
		srcPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Label dbscriptPathLabel = new Label(container, SWT.NONE);
		dbscriptPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		dbscriptPathLabel.setText("脚本目录：");

		dbscriptPathText = new Text(container, SWT.BORDER);
		dbscriptPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		containerPathLabel = new Label(container, SWT.NONE);
		containerPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		containerPathLabel.setText("容器路径：");

		containerPathText = new Text(container, SWT.BORDER);
		containerPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		parentPomPathLabel = new Label(container, SWT.NONE);
		parentPomPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		parentPomPathLabel.setText("父POM路径：");

		parentPomPathText = new Text(container, SWT.BORDER);
		parentPomPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		toolsPathLabel = new Label(container, SWT.NONE);
		toolsPathLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		toolsPathLabel.setText("工具路径：");

		toolsPathText = new Text(container, SWT.BORDER);
		toolsPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		return container;
	}

	private void addListener() {
		scmUrlText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});
		svnUserText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});
		svnPasswordText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validatePage();
			}
		});
		checkSvnButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				validatePage();
			}
		});
		headRevisionButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				endVersionText.setText("");
				endVersionText.setEditable(false);
			}
		});
		revisionButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String endVersion = projectConfig.getEndVersion();
				endVersionText.setEditable(true);
				if (AssertUtil.isNotEmpty(endVersion) && !endVersion.equals("HEAD")) {
					endVersionText.setText(endVersion);
				} else {
					endVersionText.setText("");
				}
			}
		});
	}

	private void init() {
		NewBuilderProjectWizard wizard = (NewBuilderProjectWizard) getWizard();
		projectConfig = wizard.getProjectConfig();

		setText(scmUrlText, projectConfig.getScmUrl());

		setText(scmUrlText, projectConfig.getScmUrl());
		setText(svnUserText, projectConfig.getScmUser());
		setText(svnPasswordText, projectConfig.getScmPassword());

		setText(sourcePathText, projectConfig.getSourceDirectory());
		setText(docPathPath, projectConfig.getDocDirectory());
		setText(dbscriptPathText, projectConfig.getDbScriptDirectory());
		setText(demoPathText, projectConfig.getDemoDirectory());
		setText(srcPathText, projectConfig.getSrcDirectory());
		setText(containerPathText, projectConfig.getContainerDirectory());
		setText(parentPomPathText, projectConfig.getParentPom());

		setText(toolsPathText, projectConfig.getToolsPath());

		if (projectConfig.isTestSvn()) {
			checkSvnButton.setSelection(true);
		}
		setText(startVersionText, projectConfig.getStartVersion());

		Event event = new Event();
		String endVersion = projectConfig.getEndVersion();
		if (AssertUtil.isEmpty(endVersion) || endVersion.equals("HEAD")) {
			event.widget = headRevisionButton;
			headRevisionButton.setSelection(true);
			revisionButton.setSelection(false);
			// 主动触发button点击事件
			headRevisionButton.notifyListeners(SWT.Selection, event);
			// endVersionText.setText("");
		} else {
			revisionButton.setSelection(true);
			headRevisionButton.setSelection(false);
			event.widget = revisionButton;
			revisionButton.notifyListeners(SWT.Selection, event);
			// endVersionText.setText(endVersion);
		}
	}

	public void setText(Text text, String value) {
		if (value != null) {
			text.setText(value);
		}
	}

	public boolean validate() {
		pageState = validateSvn();
		if (pageState) {
			setErrorMessage(null);
		}
		setPageComplete(pageState);
		return pageState;
	}

	private boolean validateSvn() {
		String projectName = scmUrlText.getText();
		if (AssertUtil.isEmpty(projectName)) {
			setErrorMessage("请输入SVN地址！");
			return false;
		}
		String version = svnUserText.getText();
		if (AssertUtil.isEmpty(version)) {
			setErrorMessage("请输入SVN用户名！");
			return false;
		}
		String projectPath = svnPasswordText.getText();
		if (AssertUtil.isEmpty(projectPath)) {
			setErrorMessage("请输入SVN密码！");
			return false;
		}
		return true;
	}

	public boolean testLoginSvn(ProgressMonitor monitor) {
		if (checkSvnButton.getSelection()) {
			getPageData();
			boolean result = svnManageService.checkSvnLogin(projectConfig, monitor);
			if (!result) {
				setErrorMessage("SVN登录失败，请验证SVN地址和用户名、密码");
				setPageComplete(false);
				return false;
			}
		}
		setErrorMessage(null);
		setPageComplete(true);
		return true;
	}

	public Object getPageData() {
		projectConfig.setTestSvn(checkSvnButton.getSelection());
		projectConfig.setScmUrl(scmUrlText.getText().trim());
		projectConfig.setScmUser(svnUserText.getText());
		projectConfig.setScmPassword(svnPasswordText.getText());

		projectConfig.setSourceDirectory(sourcePathText.getText().trim());
		projectConfig.setDocDirectory(docPathPath.getText().trim());
		projectConfig.setDbScriptDirectory(dbscriptPathText.getText().trim());
		projectConfig.setDemoDirectory(demoPathText.getText().trim());
		projectConfig.setContainerDirectory(containerPathText.getText().trim());
		projectConfig.setParentPom(parentPomPathText.getText().trim());
		projectConfig.setToolsPath(toolsPathText.getText().trim());

		projectConfig.setStartVersion(startVersionText.getText().trim());

		boolean selection = revisionButton.getSelection();
		if (selection) {
			projectConfig.setEndVersion(endVersionText.getText().trim());
		} else {
			projectConfig.setEndVersion(ProjectConst.HEAD);
		}

		return projectConfig;
	}

	@Override
	public boolean nextButtonPressed() {
		return super.nextButtonPressed() && testLoginSvn(new ProgressMonitor(null));
	}
}
